$('#btn1').click(function() {
    $('#fliterBox').css("display") == "block" ? $('#fliterBox').hide('slow') : $('#fliterBox').show('slow');
});
// switch and show fliterList items
for (let i = 0; i < $('#fliterListNav ul li').length; i++) {
    $('#fliterListNav ul li:eq(' + i + ')').click(function(event) {
        $('#fliterListNav ul li').removeClass('nav-actived');
        $(this).addClass('nav-actived');
        var listTable = {
            "0": [],
            "1": [],
            "2": [],
            "3": [],
            "4": []
        };

        $('#fliterList ul li').html("");
        for (x in listTable[$(this).attr("name")]) {
            $('#fliterList ul li:eq(' + x + ')').html(listTable[$(this).attr("name")][x]);
        }
    });
}
// switch and show fliterList items end

// bind click event for list items
for (let j = 0; j < $('#fliterList ul li').length; j++) {
    $('#fliterList ul li:eq(' + j + ')').click(function(event) {
        $('#fliterBox').hide('slow');
        $('#span1').text(this.innerHTML);
	});
 }
// multiple menu end

//addcolor
$(addColor);
function addColor() {
	$('#gradeTbody tr').removeClass();
	for(var i = 0; i < $('#gradeTbody tr').length; i++) {
		if(i % 4 == 0) {
			$('#gradeTbody tr').eq(i).addClass('info');
		}
		if(i % 4 == 1) {
			$('#gradeTbody tr').eq(i).addClass('success');
		}
		if(i % 4 == 2) {
			$('#gradeTbody tr').eq(i).addClass('warning');
		}
		if(i % 4 == 3) {
			$('#gradeTbody tr').eq(i).addClass('active');
		}
	}
}//addcolor end

//choose all and choose more
$(chooseAll);
function chooseAll() {
	$("#btn").click(function () {
		$("input[name='a']").prop("checked", this.checked);
	});
	$("input[name='a']").click(function () {
		var $subs = $("input[name='a']");
		$("#btn").prop("checked", $subs.length == $subs.filter(":checked").length ? true : false);
	});
}
function deleteChoose() {
	if (confirm("确定删除吗？") == true) {
		var students = [];
		$("tbody input:checked").each(function () {
			var $row = $(this).closest('tr'); // 获取包含删除按钮的行  
			var studentNum = $row.find('td:eq(1)').text().trim(); // 从第二列（学号列）获取学号  
			var data = {
				id: studentNum
			};
			students.push(data);
		});
		var json = JSON.stringify(students);
		$.ajax({
			type: 'POST',                       //请求方式
			url: '/exam-grade/deleteExamall',  //请求的URL地址
			data: json,
			processData: false,
			contentType: false,
			success: location.reload(),
			error: function (xhr, status, error) { // 添加错误处理函数  
				alert("删除失败！")
				console.error("请求失败: " + error);
			}
		})
	}
	$("#btn").prop("checked", false);
}

$('#deleteAll').on('click', function () {
	deleteChoose();
	addColor();
});
// delete choose delete more end

// delete one
function deleter() {
	if (confirm("确定删除吗？") == true) {
		var $row = $(this).closest('tr'); // 获取包含删除按钮的行  
		var examId = $row.find('td:eq(1)').text().trim(); // 从第二列（学号列）获取学号  
		var data = {
			'examId':examId
		};
		$.ajax({
			type: 'POST',                       //请求方式
			url: '/exam-grade/deletesexam',  //请求的URL地址
			data: data,
			datatype: "json",
			success: function (response) {
				location.reload();
				if (response.message == "Student ID Deleted")
					alert("删除成功！")
				if (response.message == "Student ID not exsit")
					alert("删除失败，学生ID不存在！")
			},
			error: function (xhr, status, error) { // 添加错误处理函数  
				alert("删除失败！")
				console.error("请求失败: " + error);
			}
		})
	}
}
$('.deleteOne').click(deleter);


//}// delete one end
//popup
function popup(id){
	$('#zz').css('display', 'block');
	$('#addUser').css('display', 'block');
	if(id == "addUserIn"){
		$('#addUserInform').removeClass("changeStudent");
		$('#addUserInform').addClass("addStudent");
	}
	if(id == "changeInfrom"){
		$('#addUserInform').removeClass("addStudent");
		$('#addUserInform').addClass("changeStudent");
	}
}
//popup end



function addEnter() {
		//添加至下一行
		$('table tr:last').after($('<tr><td class="checkbox m-0"><label><input type="checkbox" name="a"></label></td><td></td><td></td><td></td><td></td><td></td> <td><i class="fa fa-trash-o cur-p deleteOne"></i>/<i class="fa fa-edit changeInfrom"></i></td></tr>'));
		$('table tr:last td:eq(1)').text($('#inputNum').val());
		$('table tr:last td:eq(2)').text($('#inputName').val());
		$('table tr:last td:eq(3)').text($('#inputClass').val());
		$('table tr:last td:eq(6)').find(".deleteOne").click(deleter);
		$('table tr:last td:eq(6)').find(".changeInfrom").click(changeInfo);
		colseZz();
}


function colseZz() {
	$('#zz').css('display', 'none');
	$('#addUser').css('display', 'none');
}
//close and cancel end

function studentTabke(studentInfo){
	for(var i in studentInfo){
							$('tbody').append($('<tr><td class="checkbox m-0"><label><input type="checkbox" name="a"></label></td><td></td><td></td><td></td><td></td><td></td> <td><i class="fa fa-trash-o cur-p deleteOne"></i>/<i class="fa fa-edit changeInfrom"></i></td></tr>'));
							$('tbody tr:last td:eq(1)').html(studentInfo[i]["examId"]);
							$('tbody tr:last td:eq(2)').html(studentInfo[i]["examCourse"]);
							$('tbody tr:last td:eq(3)').html(studentInfo[i]["examTime"]);
							$('tbody tr:last td:eq(6)').find(".deleteOne").click(deleter);
							$('tbody tr:last td:eq(6)').find(".changeInfrom").click(changeInfo);
							colseZz();
							addColor();
	}
}

// 假设这是一个从服务端获取数据的函数  
$(document).ready(function fetchData() {  
    $.ajax({  
        url: '/exam-grade/getExamInfo', // 假设的API URL  
        type: 'get',  
        dataType: 'json',  
        success: function(data) {  
            // 清空表格数据（如果需要）  
            $('#gradeTbody').empty();  
  
            // 遍历数据并插入到表格中  
            data.forEach(function(exam) {  
                var row = $('<tr></tr>');  
                row.append($('<td class="checkbox m-0"><label><input type="checkbox" name="a"></label></td>'));  
                row.append($('<td></td>').text(exam.examId));  
                row.append($('<td></td>').text(exam.examCourse));  
                row.append($('<td></td>').text(exam.examTime));  
                row.append($('<td></td>').text(exam.examobArray)); 
				row.append($('<td></td>').text(exam.examsubArray)); 
				row.append($('<td></td>').text(exam.examArray)); 
                var actionsCell = $('<td></td>');  
                actionsCell.append($('<i class="fa fa-trash-o cur-p deleteOne"></i>').click(deleter));  
                actionsCell.append($('<i class="fa fa-edit changeInfrom"></i>').click(function() {  
                    changeInfo.call(this); // 确保this上下文正确  
                }));  
                row.append(actionsCell);  
                $('#gradeTbody').append(row);  
            });  
  
            // 调用添加颜色的函数  
            addColor();  
        },  
        error: function() {  
            alert('数据获取失败！');  
        }  
    });  
});  

  
